<?php
include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/Snoopy.class.php');
include_once('../lib/function.php');
include_once('/usr/local/xunsearch/sdk/php/lib/XS.php');

$redis = new Redis();
$ret = $redis->connect("localhost", "6379");  //php客户端设置的ip及端口
$redis->auth(REDIS_PASS);
$redis->select(2);

$GLOBALS['config']['master']['db_database'] = 'gouyg';
$GLOBALS['config']['slave']['db_database']  = 'gouyg';

$model = new Db;
$p = 1;
$delete_count = $update_count = $old_count = 0;
while (true) {
    try {
        $xs = new XS('booksonline');
        $index  = $xs->index;
        $search = $xs->search;
        $search->setCharset('UTF-8');
        $search->setQuery('*');

        $p = max(1, intval($p));
        $n = XSSearch::PAGE_SIZE;
        $search->setLimit($n, ($p - 1) * $n);
        $docs = $search->search();

        $count = $search->getLastCount();
        $total = $search->getDbTotal();
        echo2('count:'.$count.', total:'.$total.', '.count($docs).", {$p}/{$n}".", old/update/del:{$old_count}/{$update_count}/{$delete_count}");

        if($count == 0 || count($docs) == 0) break;

        foreach ($docs as $doc) {
            $old_book = $model->find('book', "md5id='".$doc->md5id."'");
            $local_count = (int) $redis->get('chapters:'.$doc->md5id);
            if(!$local_count) {
                $index->del($doc->md5id);
                $model->query("delete from book where id='".$old_book['id']."'");
                $model->query("delete from book_chapter where book_id='".$old_book['id']."'");
                $delete_count++;
            }
            elseif(!$doc->urls_count) {
                $data = [];
                foreach($doc as $name => $value) {
                    $data[$name] = $value;
                }
                $data['urls_count'] = $local_count;

                $doc = new XSDocument;
                $doc->setFields($data);
                
                $index->update($doc);
                $update_count++;
            } else {
                $old_count++;
            }
        }

        $p++;

    } catch (Exception $e) {
        var_dump($e->getMessage());

        sleep(5);
    }
}

